package com.atguigu.gmall.pms.controller;

import java.util.List;

import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.service.AttrService;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.common.bean.PageParamVo;

/**
 * 商品属性
 *
 * @author gangge
 * @email gangge@atguigu.com
 * @date 2023-02-09 10:16:38
 */
@Api(tags = "商品属性 管理")
@RestController
@RequestMapping("pms/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;
    /*
        200

        5xx服务器内部错误
            503 ：服务运行不稳定 稍后再试
        400:
                请求报文错误(springmvc获取接口必须的 头、请求参数...失败)
        403：
                权限问题/跨域
        405：
                请求方式错误


     */
    // /category/225?type=0
    @ApiOperation("根据分类id查询销售属性")
    @GetMapping("category/{categoryId}")
    public ResponseVo listByCategoryId(@PathVariable("categoryId")Long categoryId,
                                       @RequestParam(required = true) Integer type){
        return ResponseVo.ok(attrService.list(Wrappers.lambdaQuery(AttrEntity.class)
            .eq(AttrEntity::getCategoryId , categoryId)
            .eq(AttrEntity::getType , type)));
    }
    ///group/1
    @ApiOperation("根据属性组id查询组内属性和值列表")
    @GetMapping("/group/{attrGroupId}")
    public ResponseVo listByAttrGroupId(@PathVariable("attrGroupId")Long attrGroupId){
        return ResponseVo.ok(attrService.list(Wrappers.lambdaQuery(AttrEntity.class)
            .eq(AttrEntity::getGroupId , attrGroupId)));
    }
    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> queryAttrByPage(PageParamVo paramVo){
        PageResultVo pageResultVo = attrService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<AttrEntity> queryAttrById(@PathVariable("id") Long id){
		AttrEntity attr = attrService.getById(id);

        return ResponseVo.ok(attr);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseVo<Object> save(@RequestBody AttrEntity attr){
		attrService.save(attr);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids){
		attrService.removeByIds(ids);

        return ResponseVo.ok();
    }

}
